package com.back.demo.config;

import com.back.demo.intercepter.AdminIntercepter;
import com.back.demo.intercepter.JudgeIntercepter;
import com.back.demo.intercepter.StudentIntercepter;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;

//老版本是通过继承WebMvcConfigurerAdapter类，新版本是通过实现WebMvcConfigurer接口
@Configuration
public class IntercepterConfig implements WebMvcConfigurer {

    //引入自定义拦截器对象
    @Resource
    private StudentIntercepter studentIntercepter;

    @Resource
    private JudgeIntercepter judgeIntercepter;

    @Resource
    private AdminIntercepter adminIntercepter;

    /**
     * 重写addInterceptors方法注册拦截器   order 从小到大顺序执行
     *
     * @param registry
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //addInterceptor方法向拦截器注册器添加拦截器，addPathPatterns方法添加拦截路径匹配规则("/**"是拦截所有)，excludePathPatterns方法是设置白名单，放行哪些路径
        //registry.addInterceptor(testIntercepter).addPathPatterns("/**").excludePathPatterns("/login/*").order(1);
        registry.addInterceptor(studentIntercepter)
                .addPathPatterns("/back/student/*");
        registry.addInterceptor(judgeIntercepter)
                .addPathPatterns("/back/judge/*");
        registry.addInterceptor(adminIntercepter)
                .addPathPatterns("/back/admin/*");
    }
}